package com.geeklan.smart.ui.viewmodel.pet;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.CaseInfo;
import com.geeklan.smart.data.model.PetInfo;
import com.geeklan.smart.data.model.RequestFeedPlanDelete;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.data.model.UmengMessage;
import com.geeklan.smart.data.model.UpdateFeedRuleByDate;
import com.geeklan.smart.data.repository.RepositoryCallback;
import com.geeklan.smart.data.repository.pet.PetRepository;

import java.util.List;

public class PetViewModel extends ViewModel {
    private final PetRepository petRepository;
    private MutableLiveData<List<PetInfo>> petListLiveData = new MutableLiveData<>();
    private MutableLiveData<List<FeedRule>> feedRuleList = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> deleteFeedPlanLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> updateFeedPlanLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> addCaseLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> delCaseLiveData = new MutableLiveData<>();

    public MutableLiveData<ApiResponse> getDelCaseLiveData() {
        return delCaseLiveData;
    }

    public MutableLiveData<ApiResponse> getAddCaseLiveData() {
        return addCaseLiveData;
    }

    private MutableLiveData<ApiResponse<List<CaseInfo>>> caseListLiveData = new MutableLiveData<>();

    public MutableLiveData<ApiResponse<List<CaseInfo>>> getCaseListLiveData() {
        return caseListLiveData;
    }

    public MutableLiveData<ApiResponse> getUpdateFeedPlanLiveData() {
        return updateFeedPlanLiveData;
    }

    public MutableLiveData<ApiResponse> getDeleteFeedPlanLiveData() {
        return deleteFeedPlanLiveData;
    }

    private MutableLiveData<String> updateFeedPlanErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> addCaseErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> delCaseErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getDelCaseErrorLiveData() {
        return delCaseErrorLiveData;
    }

    public MutableLiveData<String> getAddCaseErrorLiveData() {
        return addCaseErrorLiveData;
    }

    public MutableLiveData<String> getUpdateFeedPlanErrorLiveData() {
        return updateFeedPlanErrorLiveData;
    }

    private MutableLiveData<String> deleteFeedPlanErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getDeleteFeedPlanErrorLiveData() {
        return deleteFeedPlanErrorLiveData;
    }

    public MutableLiveData<List<FeedRule>> getFeedRuleList() {
        return feedRuleList;
    }

    private MutableLiveData<String> feedRuleErrorList = new MutableLiveData<>();

    public MutableLiveData<String> getFeedRuleErrorList() {
        return feedRuleErrorList;
    }

    private MutableLiveData<ApiResponse> addPetLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> delPetLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse> updatePetLiveData = new MutableLiveData<>();

    public MutableLiveData<ApiResponse> getUpdatePetLiveData() {
        return updatePetLiveData;
    }

    public MutableLiveData<ApiResponse> getDelPetLiveData() {
        return delPetLiveData;
    }

    private MutableLiveData<String> addPetErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> delPetErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> updatePetErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getUpdatePetErrorLiveData() {
        return updatePetErrorLiveData;
    }

    public MutableLiveData<String> getDelPetErrorLiveData() {
        return delPetErrorLiveData;
    }

    public MutableLiveData<List<PetInfo>> getPetListLiveData() {
        return petListLiveData;
    }

    public MutableLiveData<ApiResponse> getAddPetLiveData() {
        return addPetLiveData;
    }

    public MutableLiveData<String> getAddPetErrorLiveData() {
        return addPetErrorLiveData;
    }

    private MutableLiveData<String> errorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> caseListErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getCaseListErrorLiveData() {
        return caseListErrorLiveData;
    }

    public MutableLiveData<String> getErrorLiveData() {
        return errorLiveData;
    }

    public PetViewModel(PetRepository petRepository) {
        this.petRepository = petRepository;
    }


    public void getPetList() {
        petRepository.petList(new RepositoryCallback<ApiResponse<List<PetInfo>>>() {
            @Override
            public void onSuccess(ApiResponse<List<PetInfo>> data) {
                if (data != null) {
                    petListLiveData.setValue(data.getData());
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void addPet(PetInfo petInfo) {
        petRepository.addPet(petInfo,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    addPetLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                addPetErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void deletePet(PetInfo petInfo) {
        petRepository.deletePet(petInfo,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    delPetLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                delPetErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void updatePet(PetInfo petInfo) {
        petRepository.updatePet(petInfo,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updatePetLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                updatePetErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getFeedRuleList(String deviceId,int repetitionPeriod,String date) {//repetitionPeriod 获取指定周几的数据,不传获取全部
        petRepository.getFeedRuleList(deviceId,repetitionPeriod,date,new RepositoryCallback<ApiResponse<List<FeedRule>>>() {
            @Override
            public void onSuccess(ApiResponse<List<FeedRule>> data) {
                if (data != null) {
                    feedRuleList.setValue(data.getData());
                }
            }

            @Override
            public void onError(Exception e) {
                feedRuleErrorList.setValue(e.getMessage());
            }
        });
    }

    public void getFeedRuleList(String deviceId) {//repetitionPeriod 获取指定周几的数据,不传获取全部
        petRepository.getFeedRuleList(deviceId,new RepositoryCallback<ApiResponse<List<FeedRule>>>() {
            @Override
            public void onSuccess(ApiResponse<List<FeedRule>> data) {
                if (data != null) {
                    feedRuleList.setValue(data.getData());
                }
            }

            @Override
            public void onError(Exception e) {
                feedRuleErrorList.setValue(e.getMessage());
            }
        });
    }

    public void updateFeedRuleByDate(UpdateFeedRuleByDate updateFeedRuleByDate) {
        petRepository.updateFeedRuleByDate(updateFeedRuleByDate,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updateFeedPlanLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                updateFeedPlanErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void confirmTimeFeed(UmengMessage umengMessage) {
        petRepository.confirmTimeFeed(umengMessage,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updateFeedPlanLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                updateFeedPlanErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void deleteFeedRule(int id) {//repetitionPeriod 获取指定周几的数据,不传获取全部
        RequestFeedPlanDelete requestFeedPlanDelete = new RequestFeedPlanDelete();
        requestFeedPlanDelete.setId(id);
        petRepository.deleteFeedRule(requestFeedPlanDelete,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    deleteFeedPlanLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                deleteFeedPlanErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void updateFeedRule(FeedRule feedRule) {//repetitionPeriod 获取指定周几的数据,不传获取全部
        petRepository.updateFeedRule(feedRule,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updateFeedPlanLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                updateFeedPlanErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void addFeedRule(FeedRule feedRule) {//repetitionPeriod 获取指定周几的数据,不传获取全部
        petRepository.addFeedRule(feedRule,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    updateFeedPlanLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                updateFeedPlanErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void pathologyList(int petId,int limit,int page) {
        petRepository.pathologyList(petId,limit,page,new RepositoryCallback<ApiResponse<List<CaseInfo>>>() {
            @Override
            public void onSuccess(ApiResponse<List<CaseInfo>> data) {
                if (data != null) {
                    caseListLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                caseListErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void addPathology(CaseInfo caseInfo) {
        petRepository.addPathology(caseInfo,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    addCaseLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                addCaseErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void deletePathology(CaseInfo caseInfo) {
        petRepository.deletePathology(caseInfo,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    delCaseLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                delCaseErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void updatePathology(CaseInfo caseInfo) {
        petRepository.updatePathology(caseInfo,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    addCaseLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                addCaseErrorLiveData.setValue(e.getMessage());
            }
        });
    }

}
